<?php

/**
 * 友情链接管理
 * @author Ice <iceinto@mallog.com.cn>
 */
class LinkAction extends LoginAction
{
    public function index()
    {

        $this->display();
    }

    public function type()
    {
        $this->getLinkTypes();
        $this->display();
    }

    /**
     * 添加链接类型
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function typeAdd()
    {
        if ($this->isPost()) {
            $LinkTypeModel = D('LinkType');
            $data = $LinkTypeModel->create();
            if ($data) {
                if ($LinkTypeModel->add($data)) {
                    $this->successMessage('添加成功！');
                } else {
                    $this->errorMessage('添加失败！');
                }
            } else {
                $this->errorMessage($LinkTypeModel->getError());
            }
        }
        $this->display();
    }

    /**
     * 修改链接类型名称
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function typeEdit()
    {
        //获取信息
        $id = $this->_get('id', 'intval');
        if (empty($id)) {
            $this->error('类型标识必须！');
        }
        $LinkTypeModel = D('LinkType');
        $type = $LinkTypeModel->find($id);
        if (empty($type)) {
            $this->error('类型不存在');
        }
        if ($this->isPost()) {

            $data = $LinkTypeModel->create();
            if ($data) {
                $data['itemid'] = $id;
                if ($LinkTypeModel->save($data)) {
                    $this->successMessage('修改成功！');
                    $type = $LinkTypeModel->find($id); //重新获取
                } else {
                    $this->errorMessage('修改失败！');
                }
            } else {
                $this->errorMessage($LinkTypeModel->getError());
            }
        }
        $this->assign('type', $type);
        $this->display();
    }

    /**
     * 链接列表
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function items()
    {
        $LinkModel = M('Link');
        $allCount = $LinkModel->count();
        $Page = $this->getPage($allCount);
        $links = $LinkModel->field('itemid,title,level,list_order')->order('itemid DESC')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('links', $links);
        $this->assign('page', $Page->show());
        $this->display();
    }

    /**
     * 添加链接
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function add()
    {
        if ($this->isPost()) {
            $LinkModel = D('Link');
            $data = $LinkModel->create();
            if ($data) {
                if ($LinkModel->add($data)) {
                    $this->successMessage('添加成功！');
                } else {
                    $this->errorMessage('添加失败！');
                }
            } else {
                $this->errorMessage($LinkModel->getError());
            }
        }
        $this->getLinkTypes();
        $this->display();
    }

    /**
     * 修改链接
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function edit()
    {
        //获取信息
        $id = $this->_get('id', 'intval');
        if (empty($id)) {
            $this->error('链接标识必须！');
        }
        $LinkModel = D('Link');
        $link = $LinkModel->find($id);
        if (empty($link)) {
            $this->error('链接不存在');
        }
        if ($this->isPost()) {

            $data = $LinkModel->create();
            if ($data) {
                $data['itemid'] = $id;
                if ($LinkModel->save($data)) {
                    $this->successMessage('修改成功！');
                    $link = $LinkModel->find($id); //重新获取
                } else {
                    $this->errorMessage('修改失败！');
                }
            } else {
                $this->errorMessage($LinkModel->getError());
            }
        }
        $this->getLinkTypes();
        $this->assign('link', $link);
        $this->display();
    }


    public function getLinkTypes()
    {
        $types = M('LinkType')->select();
        $this->assign('types', $types);
    }
}

?>
